package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import domian.Comment;
import domian.Result;
import domian.User;
import redis.clients.jedis.Jedis;
import service.impl.ReviewServiceImpl;
import service.ReviewService;
import utils.Redis;
import utils.ToolSet;
import utils.ToolUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/review/*")
@SuppressWarnings("all")
public class ReviewServlet extends BaseServlet {

    ReviewService reviewService = new ReviewServiceImpl();

    /**
     * 提交评论
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void uploadReview(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求消息
        Jedis jedis = new Jedis();
        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        ObjectMapper om = new ObjectMapper();
        String json = jedis.get(redisKey);
        //用户id
        Integer uid = null;
        String nickname = null;
        if (json != null) {
            User user = om.readValue(json, User.class);
            uid = user.getUid();
            nickname = user.getNickname();
        }
        //文件id
        String href = req.getParameter("href");
        Integer fid = null;
        try {
            fid = Integer.parseInt(href.substring(href.lastIndexOf("=") + 1));
        } catch (NumberFormatException e) {
        }
        //评论
        String review = req.getParameter("review");
        //星级数
        Integer count = Integer.parseInt(req.getParameter("count"));
        //调用service方法
        Result result = reviewService.uploadReview(nickname, new Comment(uid, fid, review, ToolUtil.dateString(new Date()), count));
        //响应消息
        ToolSet.printWriter(resp, result);
    }

    /**
     * 展示评论
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void dataReview(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求消息
        Jedis jedis = new Jedis();
        ObjectMapper om = new ObjectMapper();
        String json = null;
        Boolean dataReview = jedis.exists("dataReview");
        if (dataReview) {
            json = jedis.get("dataReview");
        } else {

        }
        //文件id
        String queryString = req.getQueryString();
        String href = req.getParameter("href");
        Integer fid = null;
        try {
            fid = Integer.parseInt(href.substring(href.lastIndexOf("=") + 1));
        } catch (NumberFormatException e) {
        }
        //第几页
        String whichPage = req.getParameter("whichPage");
        Integer wp = 1;//默认第一页
        if (whichPage != null) {
            try {
                wp = Integer.parseInt(whichPage);//点击页面按钮换页
            } catch (NumberFormatException e) {
                wp = Integer.parseInt(whichPage.substring(0, whichPage.indexOf("/")));//点击跳转至指定页面换页
            }
        }
        //每页条数
        String rows = req.getParameter("rows");
        Integer row = null;
        if (rows != null) {
            row = Integer.parseInt(rows.substring(0, rows.indexOf("条")));
        }
        //调用service方法
        Result result = reviewService.dataReview(fid, wp, row);
        //响应消息
        ToolSet.printWriter(resp, result);
    }

    /**
     * 展示文件内容
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void dataFileContent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求消息
        String href = req.getParameter("href");
        Integer fid = null;
        try {
            fid = Integer.parseInt(href.substring(href.lastIndexOf("=") + 1));
        } catch (NumberFormatException e) {
        }
        //调用service方法
        Result result = reviewService.dataFileContent(fid);
        //响应消息
        ToolSet.printWriter(resp, result);
    }

    /**
     * 展示用户信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void dataUserMsg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求消息
        Jedis jedis = new Jedis();
        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        ObjectMapper om = new ObjectMapper();
        String json = jedis.get(redisKey);
        //用户信息
        User user = null;
        if (json != null) {
            user = om.readValue(json, User.class);
        }
        //响应消息
        ToolSet.printWriter(resp, user);
    }

    /**
     * 展示积分明细
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void dataPoints(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求消息
        Jedis jedis = new Jedis();
        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        ObjectMapper om = new ObjectMapper();
        String json = jedis.get(redisKey);
        //用户id
        Integer uid = null;
        if (json != null) {
            User user = om.readValue(json, User.class);
            uid = user.getUid();
        }
        //第几页
        String whichPage = req.getParameter("whichPage");
        Integer wp = 1;//默认第一页
        if (whichPage != null) {
            try {
                wp = Integer.parseInt(whichPage);//点击页面按钮换页
            } catch (NumberFormatException e) {
                wp = Integer.parseInt(whichPage.substring(0, whichPage.indexOf("/")));//点击跳转至指定页面换页
            }
        }
        //每页条数
        String rows = req.getParameter("rows");
        Integer row = null;
        if (rows != null) {
            row = Integer.parseInt(rows.substring(0, rows.indexOf("条")));
        }
        //调用service方法
        Result result = reviewService.dataPoints(uid, wp, row);
        //响应消息
        ToolSet.printWriter(resp, result);
    }

    /**
     * 开通vip
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void liberalVip(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求消息
        Jedis jedis = new Jedis();
        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        ObjectMapper om = new ObjectMapper();
        String json = jedis.get(redisKey);
        //用户id
        Integer uid = null;
        if (json != null) {
            User user = om.readValue(json, User.class);
            uid = user.getUid();
        }
        //消耗积分
        String minus = req.getParameter("pointsMinus");
        Integer pointsMinus = Integer.parseInt(minus.substring(0, minus.length() - 2));
        //有效期(毫秒)
        long validityMs = 0;//0是永久vip
        String validity = req.getParameter("validity");
        if (!validity.equals("永久有效")) {
            validityMs = Integer.parseInt(validity.substring(3, validity.length() - 1)) * 24 * 60 * 60 * 1000l;
        }
        //获取积分
        String add = req.getParameter("pointsAdd");
        Integer pointsAdd = Integer.parseInt(add.substring(2, add.length() - 2));
        //调用service
        Result result = reviewService.liberalVip(uid, pointsMinus, validityMs, pointsAdd,req);
        //响应消息
        ToolSet.printWriter(resp, result);
    }
}
